package cc.ddknight.site.iblog.service.impl;

import cc.ddknight.site.iblog.Constant;
import cc.ddknight.site.iblog.domain.User;
import cc.ddknight.site.iblog.service.UserService;

import java.io.*;
import java.util.Properties;

public class UserServiceImpl implements UserService {
    @Override
    public boolean checkUserLogin(String email, String password) {
        Boolean result = Boolean.FALSE;
        File file = new File(Constant.getUserDataFilePath() + File.separator + email);
        try {
            InputStream is = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(is);
            var realPassword = properties.get("password");
            if (String.valueOf(password).equals(realPassword)) {
                result = Boolean.TRUE;
                System.out.println("Email:"+email+",登录成功.");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Email:"+email+",找不到用户文件.");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("读取用户文件失败.");
        }
        return result;
    }

    @Override
    public User getUserByEmail(String email) {
        User user = null;
        File file = new File(Constant.getUserDataFilePath() + File.separator + email);
        try {
            InputStream is = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(is);
            String username=properties.getProperty("username");
            String password=properties.getProperty("password");
            String memo=properties.getProperty("memo");
            user = new User(username,password,email,memo);
            System.out.println("登录用户信息如下:"+user.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("找不到用户文件.");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("读取用户文件失败.");
        }
        return user;
    }
}
